#include <iostream>
#include <boost/cstdint.hpp>
#include <boost/asio.hpp>
#include <mysql.h>
#include "tcp_server.h"
#include "mysql_session.h"

#include "globals.h"

using namespace std;
using namespace boost;

MYSQL *mysql;

int main(int argc, char *argv[])
{
	const char *server = "localhost";
	const char *user = "egear";
	const char *password = "egear";
	const char *database = "egear";
	const int16_t port = 8899;

	if (mysql_library_init(argc, argv, NULL))
	{
		cerr << "Failed to initialize MYSQL library\n";
		exit(1);
	}
	if ((mysql = mysql_init(NULL))==NULL)
	{
		cerr << "Failed to initialize MYSQL connection\n";
		exit(1);
	}
	
	if (!mysql_real_connect(mysql, server,
		user, password, database, 0, NULL, 0)) {
		cerr 	<< "Failed to connect to mysql server: "
			<< mysql_error(mysql) << endl;
		exit(1);
	}
	
	boost::asio::io_service io_service;
	MysqlSession::pointer mysqlSession
		= MysqlSession::pointer(new MysqlSession(io_service));
	TcpServer tcpServer(io_service, mysqlSession, port);

	io_service.run();
	mysql_close(mysql);
	delete mysql;
	mysql_library_end();
}
